package com.ww.springboot.boot.thread.study.base.semaphore;

import com.ww.springboot.boot.thread.study.base.lock.aqs.util.ThreadPrintUtil;

import java.util.concurrent.Semaphore;

/**
 * 描述：
 *
 * 同步队列即可解决 设置一个上限值 每次加锁 或解锁都利用cas来更新state 如果state>0 就允许获取锁
 *
 * @author 🧑 ‍wanwei
 * @since 2022-04-20 17:13
 */
public class 信号量Demo1 {

    /**
     * 相当于令牌桶
     */
    private static final Semaphore s = new Semaphore(3);

    public static void main(String[] args) throws InterruptedException {

        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ThreadPrintUtil.sout("释放值");
                s.release();
            }

        }).start();

        for (int i = 0; i < 10; i++) {
            System.out.println(i + "" + System.currentTimeMillis());
            ThreadPrintUtil.sout("塞值");
            s.acquire();
            Thread.sleep(1000);
        }


    }
}
